import Toast from '../../../dist/toast/toast';
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeNames: ['1','2','3'],
    petName:"",
    wxNum:"",
    qqNum:"",
    userInfo: {
      avatarUrl: null,
      nickName: null
    }
  },
  handlerPetname: function (e) {
    this.setData({
      petName: e.detail.value
    })
  }, handlerWxnumb: function (e) {
    this.setData({
      wxNum: e.detail.value
    })
  }, handlerQqnumb: function (e) {
    this.setData({
      qqNum: e.detail.value
    })
  },
  handleSubmit() {
    let self = this;
    let tEL_REGEXP = /^1([38]\d|5[0-35-9]|7[3678])\d{8}$/; 
    let qQ_REGEXP = /^[1-9][0-9]{4,9}$/;
    if (self.data.petName==""){
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请输入用户昵称',
        success(res) {
          if (res.confirm) {
            return;
          }
        }
      })
    }else if (!tEL_REGEXP.test(self.data.wxNum)){
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请输入正确微信绑定手机',
        success(res) {
          if (res.confirm) {
            return;
          }
        }
      })
    }else if (!qQ_REGEXP.test(self.data.qqNum)) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请输入正确QQ号码',
        success(res) {
          if (res.confirm) {
            return;
          }
        }
      })
    }else{
      Toast.loading({
        mask: true,
        message: '上传中...'
      });
      let openid = wx.getStorageSync("openid")
      db.collection('user').where({
        '_openid': openid
      }).get({
        success(res) {
          console.log(res.data.length)
          if (res.data.length) {
            db.collection('user').doc(res.data[0]._id).update({
              data: {
                date: (new Date()).valueOf(),
                petName: self.data.petName,
                wxNum: self.data.wxNum,
                qqNum: self.data.qqNum
              }
            }).then(res => {
              wx.showModal({
                title: '提示',
                showCancel: false,
                content: '上传成功',
                success(res) {
                  if (res.confirm) {
                    wx.navigateBack({
                      delta: 1
                    })
                  }
                }
              })
            })
          } else {

            db.collection('user').add({
              // data 字段表示需新增的 JSON 数据
              data: {
                username: self.data.userInfo.nickName,
                userpic: self.data.userInfo.avatarUrl,
                date: (new Date()).valueOf(),
                petName: self.data.petName,
                wxNum: self.data.wxNum,
                qqNum: self.data.qqNum
              }
            })
              .then(res => {
                wx.showModal({
                  title: '提示',
                  showCancel: false,
                  content: '上传成功',
                  success(res) {
                    if (res.confirm) {
                      wx.navigateBack({
                        delta: 1
                      })
                    }
                  }
                })
              })
          }

        }
      })
    }
  
      
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self=this
    wx.getSetting({
      success: res => {
        wx.getUserInfo({
          success: res => {
            this.setData({
              userInfo: {
                avatarUrl: res.userInfo.avatarUrl,
                nickName: res.userInfo.nickName
              }
            })
          }
        })

      }
    })
    let openid = wx.getStorageSync("openid")
    db.collection('user').where({
      '_openid':openid
    }).get({
      success(res) {
        console.log(res)
        self.setData({
          petName: res.data[0].petName,
          wxNum: res.data[0].wxNum,
          qqNum: res.data[0].qqNum
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})